namespace PickGold.Charting.Design
{
    using System;
    using System.Windows.Forms;

    internal class FlagsEnumCheckedListBox : CheckedListBox
    {
        private System.Type _editType;
        private object _editValue;

        public FlagsEnumCheckedListBox(object editValue, System.Type editType)
        {
            this._editValue = editValue;
            this._editType = editType;
            base.BorderStyle = BorderStyle.None;
            this.FillList();
        }

        private void FillList()
        {
            if (!this._editType.IsEnum)
            {
                throw new ArgumentException(SR.ExceptionEditorUITypeEditorInapplicable);
            }
            if (Enum.GetUnderlyingType(this._editType) != typeof(int))
            {
                throw new ArgumentException(SR.ExceptionEditorUITypeEditorInt32ApplicableOnly);
            }
            int num = 0;
            if (this._editValue != null)
            {
                num = (int) this._editValue;
            }
            foreach (object obj2 in Enum.GetValues(this._editType))
            {
                int num2 = (int) obj2;
                if (num2 != 0)
                {
                    bool isChecked = (num & num2) == num2;
                    base.Items.Add(Enum.GetName(this._editType, obj2), isChecked);
                }
            }
        }

        public object GetNewValue()
        {
            int num = 0;
            foreach (object obj2 in base.CheckedItems)
            {
                int num2 = (int) Enum.Parse(this._editType, (string) obj2);
                num |= num2;
            }
            return Enum.ToObject(this._editType, num);
        }
    }
}

